package com.fw.ui.view.common.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.Scope;

import com.fw.ui.view.common.TwinListSelectPanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
public class TwinListSelectPanelRemoveButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 2357909901769737080L;

    private TwinListSelectPanel twinListSelectPanel;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	Set<Object> selectedObject = (Set<Object>) this.twinListSelectPanel.getSelectCurrent().getValue();
	if (selectedObject != null && !selectedObject.isEmpty()) {
	    for (Object obj : selectedObject) {
		String caption = this.twinListSelectPanel.getSelectCurrent().getItemCaption(obj);
		this.twinListSelectPanel.getCurrentSelectedMap().remove(obj);
		this.twinListSelectPanel.getAvailableMap().put(obj, caption);
	    }
	    String textCurrent = (String) this.twinListSelectPanel.getTxtCurrentSelected().getValue();
	    if (textCurrent != null && textCurrent.trim().length() > 0) {
		Map<Object, String> newMap = new HashMap<Object, String>();
		Set<Object> keySet = this.twinListSelectPanel.getCurrentSelectedMap().keySet();
		Iterator<Object> keyIterator = keySet.iterator();
		while (keyIterator.hasNext()) {
		    Object key = keyIterator.next();
		    String str = this.twinListSelectPanel.getCurrentSelectedMap().get(key);
		    if (str.toLowerCase().startsWith(textCurrent.toLowerCase())) {
			newMap.put(key, str);
		    }
		}
		this.twinListSelectPanel.setListSelectData(newMap, this.twinListSelectPanel.getSelectCurrent());
	    } else {
		this.twinListSelectPanel.setListSelectData(this.twinListSelectPanel.getCurrentSelectedMap(),
			this.twinListSelectPanel.getSelectCurrent());
	    }
	    String textAvailable = (String) this.twinListSelectPanel.getTxtAvailable().getValue();
	    if (textAvailable != null && textAvailable.trim().length() > 0) {
		Map<Object, String> newMap = new HashMap<Object, String>();
		Set<Object> keySet = this.twinListSelectPanel.getAvailableMap().keySet();
		Iterator<Object> keyIterator = keySet.iterator();
		while (keyIterator.hasNext()) {
		    Object key = keyIterator.next();
		    String str = this.twinListSelectPanel.getAvailableMap().get(key);
		    if (str.toLowerCase().startsWith(textAvailable.toLowerCase())) {
			newMap.put(key, str);
		    }
		}
		this.twinListSelectPanel.setListSelectData(newMap, this.twinListSelectPanel.getSelectAvailable());
	    } else {
		this.twinListSelectPanel.setListSelectData(this.twinListSelectPanel.getAvailableMap(),
			this.twinListSelectPanel.getSelectAvailable());
	    }
	}
	return null;
    }

    @Override
    public void setBasedComponent(Component basedComponent) {
	this.twinListSelectPanel = (TwinListSelectPanel) basedComponent;
    }

    @Override
    public boolean validate(ClickEvent event) throws Exception {
	return true;
    }

}
